package com.ruoyi.bantong.controller;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bantong.domain.BtPlacecardGroup;
import com.ruoyi.bantong.service.IBtPlacecardGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 红包包组Controller
 * 
 * @author laohei
 * @date 2021-03-26
 */
@RestController
@RequestMapping("/bantong/group")
public class BtPlacecardGroupController extends BaseController
{
    @Autowired
    private IBtPlacecardGroupService btPlacecardGroupService;

    /**
     * 查询红包包组列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(BtPlacecardGroup btPlacecardGroup)
    {
        startPage();
        List<BtPlacecardGroup> list = btPlacecardGroupService.selectBtPlacecardGroupList(btPlacecardGroup);
        return getDataTable(list);
    }

    /**
     * 导出红包包组列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:group:export')")
    @Log(title = "红包包组", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BtPlacecardGroup btPlacecardGroup)
    {
        List<BtPlacecardGroup> list = btPlacecardGroupService.selectBtPlacecardGroupList(btPlacecardGroup);
        ExcelUtil<BtPlacecardGroup> util = new ExcelUtil<BtPlacecardGroup>(BtPlacecardGroup.class);
        return util.exportExcel(list, "group");
    }

    /**
     * 获取红包包组详细信息
     */
    //@PreAuthorize("@ss.hasPermi('bantong:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(btPlacecardGroupService.selectBtPlacecardGroupById(id));
    }

    /**
     * 查询红包包组 将要获得的金额
     */
    //@PreAuthorize("@ss.hasPermi('bantong:group:query')")
    @GetMapping(value = "/getMoney")
    public AjaxResult getMoney( Integer level,Integer userId)
    {
        try {
            return AjaxResult.success(btPlacecardGroupService.getBtPlacecardGroupByLevel(level,userId));
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof CustomException){
                return AjaxResult.error(e.getMessage());
            }else {
                return AjaxResult.error();
            }
        }
    }

    /**
     * 新增红包包组
     */
    //@PreAuthorize("@ss.hasPermi('bantong:group:add')")
    @Log(title = "红包包组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BtPlacecardGroup btPlacecardGroup)
    {
        return toAjax(btPlacecardGroupService.insertBtPlacecardGroup(btPlacecardGroup));
    }

    /**
     * 修改红包包组
     */
    //@PreAuthorize("@ss.hasPermi('bantong:group:edit')")
    @Log(title = "红包包组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BtPlacecardGroup btPlacecardGroup)
    {
        return toAjax(btPlacecardGroupService.updateBtPlacecardGroup(btPlacecardGroup));
    }

    /**
     * 删除红包包组
     */
    //@PreAuthorize("@ss.hasPermi('bantong:group:remove')")
    @Log(title = "红包包组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(btPlacecardGroupService.deleteBtPlacecardGroupByIds(ids));
    }
}
